package soa.security.web.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;

import soa.security.domain.Account;
import soa.security.domain.Applsystem;
import soa.security.domain.Organization;
import soa.security.mgr.service.AccountMgrService;
import soa.security.mgr.service.ApplsystemMgrService;
import soa.security.mgr.service.OrganizationMgrService;
import soa.security.web.LdapAuthenticationFilter;
import v1.security.domain.biz.Menu;
import v1.security.service.PrivilegeService;
import v1.web.action.ProjectEntryAction;

/**
 * 项目入口相关的页面，同时也是传统Spring+Struts2+JPA开发的示例程序
 * 
 * @author wangs
 * 
 */

@Namespace("/entry/ria")
public class RiaProjectEntryAction extends ProjectEntryAction {
	private static final long serialVersionUID = -8076477222214765363L;

	@Resource
	protected PrivilegeService privilegeService;
	@Resource
	protected OrganizationMgrService organizationMgrService;
	@Resource
	protected AccountMgrService accountMgrService;
	@Resource
	protected ApplsystemMgrService applsystemMgrService;

	protected List<Menu> menus;
	protected List<Organization> organizations;

	@Override
	@Action("login")
	public String login() {
		organizations = organizationMgrService.allOrganizations4Login();
		return SUCCESS;
	}
	
	@Action("applsystem")
	public String applsystem() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession(false);
		Account account = (Account)(session.getAttribute(LdapAuthenticationFilter.SESSION_ACCOUNT_KEY));
		
		String organizationId = "";
		
		//获取所有系统目录
		List<Applsystem> allapplsystemlist = new ArrayList<Applsystem>();
		allapplsystemlist = applsystemMgrService.allApplsystems_region_unit(null, null);
		
		//获取当前登陆用户关联的系统目录
		List<Applsystem> applsystemlist = new ArrayList<Applsystem>();
		if (account.getOrganization() != null) {
			organizationId = account.getOrganization().getBid();
			applsystemlist = accountMgrService.allApplsystems(organizationId, account.getUid());
		} else {
			//登陆用户获取不到所属机构，那么默认获取登录账户的关联系统目录(admin)
			applsystemlist = accountMgrService.allApplsystems(account);
		}
		
		//标记页面是否可以点击状态
		for (Applsystem applsystem : allapplsystemlist) {
			for (Applsystem appl : applsystemlist) {
				if (applsystem.getBid().equals(appl.getBid())) {
					applsystem.setStatus("1");
					break;
				}
			}
		}
		
		//增加登录时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		account.setLoginTime(format.format(new Date()));
		account.setOrganizationId(organizationId);
		accountMgrService.updateLoginAccount(organizationId, account); 
		
		request.setAttribute("allapplsystemlist", allapplsystemlist);
		return SUCCESS;
	}

	@Override
	@Action("v1")
	public String container() {
		return SUCCESS;
	}

	public List<Menu> getMenus() {
		return menus;
	}

	public void setMenus(List<Menu> menus) {
		this.menus = menus;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}
}
